package com.javatian.controller;

import com.github.pagehelper.PageInfo;
import com.javatian.common.Result;
import com.javatian.entity.Account;
import com.javatian.entity.MessageInfo;
import com.javatian.service.MessageInfoService;
import com.javatian.vo.MessageInfoVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 留言评价信息控制器
 */
@RestController
@RequestMapping(value = "/messageInfo")
public class MessageInfoController {

    //用户信息
    @Value("${const.user}")
    private String user;

    @Resource
    private MessageInfoService messageInfoService;


    /**
     * 新增一个留言评价信息
     */
    @PostMapping
    public Result<MessageInfo> add(@RequestBody MessageInfo messageInfo,HttpServletRequest request){
        try{
            Account account = (Account) request.getSession().getAttribute(user);
            return Result.success( messageInfoService.add(messageInfo,account));
        }catch (Exception e){
            return Result.error("1001","请先登录");
        }

    }

    /**
     * 根据主键id删除一个留言评价信息
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        messageInfoService.delete(id);
        return Result.success();
    }

    /**
     * 修改一个留言评价信息
     */
    @PutMapping
    public Result update(@RequestBody MessageInfo messageInfo){
        messageInfoService.update(messageInfo);
        return Result.success();
    }

    /**
     * 根据主键id获取一个留言评价信息对象
     */
    @GetMapping("/{id}")
    public Result<MessageInfo> detail(@PathVariable Long id){
        MessageInfo messageInfo = messageInfoService.findById(id);
        return Result.success(messageInfo);
    }

    /**
     * 查询所有留言评价信息记录
     */
    @GetMapping
    public Result<List<MessageInfoVo>> all(){
        return Result.success(messageInfoService.findAll());
    }



    /**
     * 根据条件分页查询
     * @param pageNum 第几页
     * @param pageSize 每页数量
     * @return 当前页的List列表
     */
    @GetMapping("/page/{name}")
    public Result<PageInfo<MessageInfoVo>> page(@PathVariable String name,
                                                      @RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                                      @RequestParam(required = false,defaultValue = "5") Integer pageSize,
                                                      HttpServletRequest request){
        return Result.success(messageInfoService.findPage(name,pageNum,pageSize));
    }


}
